//
//  FZHomeStatusViewModel.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/24/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZHomeStatusViewModel: NSObject {
    
    var status: FZHomeStatus?
    var name: String?
    
    var source: String?
    var vipIMG: UIImage?
    var avatarIMG: UIImage?
    var content: String?
    
    init(status: FZHomeStatus) {
        super.init()
        self.status = status
        name = getName()
        
        source = getSource()
        vipIMG = getVIPIMG()
        avatarIMG = getAvatarIMG()
        content = getCountent()
        
        
    }
//MARK:==========================方法==========================
    private func getName() -> String {
        guard let name = status?.user?.name else{
            return ""
        }
        return name
    }
    
    //MARK: 时间转换
//    private func getTime() -> String {
//        
//        guard let timeString = status?.created_at else{
//            return ""
//        }
//       
//        formatedTime(timeString)
//
//        return formatedTime(timeString)
//    }
    private func getSource() -> String {

        guard let source = status?.source else{
            return ""
        }
        if source.characters.count == 0{
            return ""
        }
        let range: Range = source.rangeOfString(">")!
        let range1 = (source.substringFromIndex(range.endIndex).rangeOfString("<"))!
        let sc = "来自 \(source.substringFromIndex(range.endIndex).substringToIndex(range1.startIndex))"
        
        return sc
    }
    private func getCountent() -> String {
        guard let content = status?.text else{
            return ""
        }
        return content
    }
    private func getVIPIMG() -> UIImage {
        
        guard let vip_i = status?.user?.mbrank else{
            return UIImage(named: "common_icon_membership_level0")!
        }
        
        return UIImage(named: "common_icon_membership_level\(vip_i)")!
    }
    private func getAvatarIMG() -> UIImage {
        
        guard let avatar_i = status?.user?.verified_type else{
            
            return UIImage()
            
        }
        let img: UIImage
        switch avatar_i {
        case 0:
            img = UIImage(named: "avatar_vip")!
            break
        case 2,3,5:
            img = UIImage(named: "avatar_enterprise_vip")!
            break
        case 220:
            img = UIImage(named: "avatar_grassroot")!
            break
        default:
            img = UIImage()
        }
        return img
    }
}
